package com.seanLab.tool.TagSuggestModel.ExpandRank.common;

import org.ansj.domain.Result;
import org.ansj.domain.Term;
import org.ansj.splitWord.Analysis;
import org.ansj.splitWord.analysis.DicAnalysis;
import org.ansj.splitWord.analysis.IndexAnalysis;
import org.ansj.splitWord.analysis.NlpAnalysis;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.thunlp.language.chinese.WordSegment;

import java.util.Iterator;

import static com.seanLab.tool.TagSuggestModel.StaticModelProperties.DefaultFilter;

/**
 * Anaj分词器
 */
public class AnsjSegment implements WordSegment{
    Analysis analysis;

    public AnsjSegment() {
//        analysis = new NlpAnalysis();
        analysis = new DicAnalysis();
//        analysis = new IndexAnalysis();
//        analysis = new ToAnalysis();
    }

    @Override
    public boolean outputPosTag() {
        return false;
    }

    @Override
    public String[] segment(String s) {
        Result result = analysis.parseStr(s).recognition(DefaultFilter);
        Iterator<Term> iter = result.iterator();
        String[] ret = new String[result.size()];
        int cnt = 0;
        while (iter.hasNext()) {
            ret[cnt++] = iter.next().getName();
        }
        return ret;
    }

    public String segmentToString(String s) {
        return analysis.parseStr(s).recognition(DefaultFilter).toString(" ");
    }
}
